home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / DELPHI.SWG / 0031_SQL: Embedded Spaces in Field-Column Nam.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  3.2 KB  |  96 lines

  1.  
  2. Implementing SQL with spaces or special characters in field/column names
  3.  
  4. Implementing SQL statements in Delphi's TQuery component (or the
  5. SQL query facilities of Database Desktop, Visual dBASE or Paradox
  6. for Windows) requires special syntax for any columns that contain
  7. spaces or special characters.
  8.  
  9. Using the Biolife.DB table of from Delphi's demo data to
  10. illustrate, and without the use of any special syntax
  11. requirements, a SQL Select statement might be formed as follows,
  12.  
  13. SELECT
  14.  Species No,
  15.  Category,
  16.  Common_Name,
  17.  Species Name,
  18.  Length (cm),
  19.  Length_In,
  20.  Notes,
  21.  Graphic
  22. FROM
  23.  BIOLIFE
  24.  
  25. While appearing normal, the space in the species number and name
  26. columns and the column expressing length in centimeters - as well
  27. as the parentheses present - cause syntax errors.
  28.  
  29. Two changes must be taken to correct the syntax of the above SQL
  30. statement.  First, any columns containing spaces or special
  31. characters must be surrounded by single (apostrophe) or double
  32. quotes.  Secondly, a table reference and a period must precede
  33. the quoted column name.  This second requirement is particularly
  34. important since a quoted string alone is interpreted as a string
  35. expression to be yielded as a column value.  A properly formatted
  36. statement follows:
  37.  
  38. SELECT
  39.  BIOLIFE."Species No",
  40.  BIOLIFE."Category",
  41.  BIOLIFE."Common_Name",
  42.  BIOLIFE."Species Name",
  43.  BIOLIFE."Length (cm)",
  44.  BIOLIFE."Length_In",
  45.  BIOLIFE."Notes",
  46.  BIOLIFE."Graphic"
  47. FROM
  48.  "BIOLIFE.DB" BIOLIFE
  49.  
  50. The above example uses the table alias BIOLIFE as the table
  51. reference that precedes the column name.  This reference may take
  52. the form of an alias name, the actual table name, or a quoted
  53. file name when using dBASE or Paradox tables.  The following
  54. SQL statements would serve equally well.
  55.  
  56. Note: This SQL statement may be used provided that the necessary
  57. alias is already opened.  In the case of the TQuery this means the
  58. alias is specified in the DatabaseName property.
  59.  
  60. SELECT
  61.  BIOLIFE."Species No",
  62.  BIOLIFE.Category,
  63.  BIOLIFE.Common_Name,
  64.  BIOLIFE."Species Name",
  65.  BIOLIFE."Length (cm)",
  66.  BIOLIFE.Length_In,
  67.  BIOLIFE.Notes,
  68.  BIOLIFE.Graphic
  69. FROM
  70.  BIOLIFE
  71.  
  72. If an alias is not available then the entire path to the table
  73. can be specified as in this example:
  74.  
  75. SELECT
  76.  "C:\DELPHI\DEMOS\DATA\BIOLIFE.DB"."Species No",
  77.  "C:\DELPHI\DEMOS\DATA\BIOLIFE.DB"."Category",
  78.  "C:\DELPHI\DEMOS\DATA\BIOLIFE.DB"."Common_Name",
  79.  "C:\DELPHI\DEMOS\DATA\BIOLIFE.DB"."Species Name",
  80.  "C:\DELPHI\DEMOS\DATA\BIOLIFE.DB"."Length (cm)",
  81.  "C:\DELPHI\DEMOS\DATA\BIOLIFE.DB"."Length_In",
  82.  "C:\DELPHI\DEMOS\DATA\BIOLIFE.DB"."Notes",
  83.  "C:\DELPHI\DEMOS\DATA\BIOLIFE.DB"."Graphic"
  84. FROM
  85.  "C:\DELPHI\DEMOS\DATA\BIOLIFE.DB"
  86.  
  87. Finally, two facilities that automatically handle this special
  88. formatting exist.  The first is the Visual Query Builder that is
  89. a part of the Client/Server version of Delphi.  The Visual Query
  90. Builder performs this formatting automatically as the query is built.
  91. The other facility is Database Desktop's Show SQL feature, available
  92. when creating or modifying a QBE-type query.  After selecting
  93. Query|Show SQL from the main menu, the displayed SQL text may be
  94. cut and pasted where needed.
  95.  
  96.